<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%>
<%@ include file="../../../../header.jsp"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<style type="text/css">
.set-status {
	
}
</style>
</head>
<body>
	<div class="mod-toolbar-top cf" style="margin-top: 10px;">
	
		<div class="fr" style="margin-top: 30px;">
			<div style="display: inline;">
				在职情况: <select id="isActive">
					<option value="0">全部</option>
					<option value="1">在职</option>
					<option value="2">离职</option>
				</select>
			</div>
			&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp
			&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp
			&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp

			<input value="" placeholder="姓名" class="ui-input" name="pname"
				id="pname" style="width: 100px; height: auto;" type="text">
			-<input value="" placeholder="公司" class="ui-input"
				style="width: 100px; height: auto;" name="cname" id="cname"
				type="text"> -<select style="width: 130px; height: auto;"
				class="ui-input" name="dname" id="dname">
				<option value="">选择要查找的部门</option>
				<s:forEach items="${departments}" var="department">
					<option value="${department.deptName}">${department.deptName}</option>
				</s:forEach>
			</select> &nbsp&nbsp<a class="ui-btn ui-btn-search" id="search">查询</a>&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp


			<a href="javascript:void();" class="ui-btn ui-btn-sp mrb"
				id="btn-add">新增</a> <a href="javascript:void();"
				class="ui-btn ui-btn-sp mrb" onclick="exportExcel()">导出excel</a> 
				<a href="javascript:void();"
				class="ui-btn ui-btn-sp mrb" onclick="importExcel()">导入excel</a>
				<a
				href="javascript:void(0);" class="ui-btn" id="btn-record">刷新</a> <a
				class="ui-btn mrb" href="/ownshiroredis/logout">退出</a>
		</div>
	</div>
	<div class="grid-wrap" align="left" style="margin-left: 10px;">
		<table id="grid1"></table>
	</div>
	<div>
		<input type="file" name="excelFile" style="opacity: 0;" id="importFile" onchange="upload()">
	</div>
	

	<script type="text/javascript">
	var compid;
    var parentname;
	var defaultCompanyId=1;
	var companyId =1
		function update() {
			$.ajax({
				url : "update",
				dataType : "json",
				success : function(data) {
					
				},
				erro : function() {
					alert("爆炸了！");
				}

			});
		}
		function deletes() {
			$.ajax({
				url : "delete",
				dataType : "json",
				success : function(data) {

				},
				erro : function() {
					alert("爆炸了！");
				}
			});
		}
		function clearGrid() {
			$("#grid").jqGrid('clearGridData');
		}
		function statusFmatter(cellValue, options, rowObject) {
			if(cellValue==1){
				return '<span  class="set-status open" data-delete='+cellValue+' data-id='+options.rowId+'><span class="ui-icon-circle"></span></span>'
			}else if(cellValue==0){
				return '<span  class="set-status close" data-delete='+cellValue+' data-id='+options.rowId+'><span class="ui-icon-circle"></span></span>'
			}
		}
		
		//添加图标
		function operationFormat(cellValue, options, rowObject) {
			return '<div class="operating" data-id="' + options.rowId + 
			'"><span class="ui-icon ui-icon-pencil" title="编辑" data-toggle="modal" data-target="#myModal"></span>'
			+ '<span id="bumen" class="ui-icon ui-icon-folder-open" title="查看部门调动"></span>'		
			+ '<span class="ui-icon ui-icon-trash" title="删除"></span>'; 
		}
		var checkBoxIdGen=0;
		function booleanFormater(cellValue,option,rowObject){
			checkBoxIdGen++;
			return ' <input disabled=true class="switch-simple-3" id="'+option.colModel.name+'-'+checkBoxIdGen+'" type="checkbox" '+((cellValue==true)?'checked':'')+'><label for="'+option.colModel.name+'-'+checkBoxIdGen+'" class="green"></label>';
		}
		function mycallback(data) {
			if(data!=undefined){
				$('#grid1').jqGrid('setGridParam',{search:true});  
				$("#grid1").jqGrid("setGridParam",{postData:data}).jqGrid("setGridParam",{page:1}).trigger("reloadGrid");
			}else{
				$("#grid1").jqGrid().trigger("reloadGrid");
			}
		}
		$("#isActive").change(function(){
			var searchField= "active";
			var searchString=$("#isActive").val();
			var data={searchField:searchField,searchString:searchString,searchOper:"eq"};
			mycallback(data);
		});
		//导入excel表格
		function exportExcel(){
			var flag = window.confirm("确定要导出数据吗？(根据员工在职情况导出)");
			if(flag){
				window.location.href="/employee/exportExcel?isActive="+$("#isActive").val();
			}
		}
		
		function importExcel(){
			$("#importFile").click();
		}
		function upload(){
			var formData = new FormData();
			var uploadFile = $('#importFile').get(0).files[0];
			formData.append("excelFile", uploadFile);
			$.ajax({
				url : 'employee/readExcel',
				type : 'POST',
				dataType : "json",
				data : formData,
				async : false,
				cache : false,
				contentType : false, //不设置内容类型  
				processData : false, //不处理数据  
				success : function(data) {
					if (data.status == 200) {
						alert("数据导入成功！");
						$("#grid1").jqGrid().trigger("reloadGrid");
					} else {
						alert("数据导入失败："+data.message);
					}
				},
				error : function(XMLHttpRequest, textStatus,
						errorThrown) {
					console.log(XMLHttpRequest.status);
					console.log(XMLHttpRequest.readyState);
					console.log(textStatus);
				}
			});
		}
		
		$(function() {
			$("#grid1").jqGrid(
					{
						url : "employee/list",
						datatype : "json",
						height : 400,
						width : '100%',
						shrinkToFit : false,
						autoScroll : false,
						rownumbers : true,
						rowNum : -1,
						cellEdit : true,
						cellsubmit : "clientArray",
						colNames : [ "操作", "ID","公司名称","部门","工号", "员工姓名","英文名称","性别","名族","婚姻","职位","身份证号","身份证地址","现居住地",
							"出生日期","毕业日期","入职日期","转正日期","转正标志","劳动合同到期日","劳动合同标志","离职时间","学历","毕业院校","专业","英语",
							"银行卡号","公积金","社保号","金融社保卡号","工卡","港澳通行证","紧急联系人","紧急电话","邮箱","手机","微信","QQ号","备注","创建时间","修改时间","在职?"],
						colModel : [ {
							name : "operating",
							width : 100,
							formatter : operationFormat,
							align : "center",
							sortable : false
						}, {
							name : "id",
							index : "id",
							key : true,
							editable : false,
							sorttype : "int",
							align : "center",
							width : 100
						}, {
							name : 'companyid',
							width : 100,
							align : "center",
						},{
							name : 'deptid',
							width : 100,
							align : "center"
						}, {
							name : 'personcode',
							align : "center",
							width : 100
						}, {
							name : 'personname',
							align : "center",
							width : 100
						}, {
							name : 'personename',
							align : "center",
							width : 100
						}, {
							name : 'sexid',
							align : "center",
							width : 100
						},{
							name : 'nationid',
							width : 100,
							align : "center"
						},{
							name : 'marriageid',
							width : 100,
							align : "center"
						},{
							name : 'postid',
							width : 100,
							align : "center"
						},{
							name : 'identityno',
							width : 200,
							align : "center"
						},{
							name : 'homeaddr',
							width : 300,
							align : "center"
						},{
							name : 'nowaddr',
							width : 300,
							align : "center"
						},{
							name : 'birthdate',
							width : 100,
							align : "center"
						},{
							name : 'graduatedate',
							width : 100,
							align : "center"
						},{
							name : 'officedate',
							width : 100,
							align : "center"
						},{
							name : 'memberdate',
							width : 100,
							align : "center"
						},{
							name : 'ismembered',
							width : 100,
							align : "center",
							formatter : booleanFormater
						},{
							name : 'contractdate',
							width : 100,
							align : "center"
						},
						
						{
							name : 'iscontracted',
							width : 100,
							align : "center",
							formatter : booleanFormater
						},{
							name : 'leavedate',
							width : 100,
							align : "center"
						},{
							name : 'educationdegree',
							width : 100,
							align : "center"
						},{
							name : 'school',
							width : 100,
							align : "center"
						},{
							name : 'major',
							width : 100,
							align : "center"
						},{
							name : 'englishdegreeid',
							width : 100,
							align : "center"
						},{
							name : 'bankCardNo',
							width : 100,
							align : "center"
						},{
							name : 'providentFund',
							width : 100,
							align : "center"
						},{
							name : 'insuranceno',
							width : 100,
							align : "center"
						},{
							name : 'bankinsuranceno',
							width : 100,
							align : "center"
						},{
							name : 'companycardno',
							width : 100,
							align : "center"
						},{
							name : 'hkmcpassport',
							width : 100,
							align : "center"
						},{
							name : 'iceperson',
							width : 100,
							align : "center"
						},{
							name : 'icetel',
							width : 100,
							align : "center"
						},{
							name : 'email',
							width : 100,
							align : "center"
						},{
							name : 'mobile',
							width : 100,
							align : "center"
						},{
							name : 'weixin',
							width : 100,
							align : "center"
						},{
							name : 'qq',
							width : 100,
							align : "center"
						},{
							name : 'remark',
							width : 100,
							align : "center"
						},{
							name : 'createtime',
							width : 100,
							align : "center"
						},{
							name : 'updatetime',
							width : 100,
							align : "center"
						},{
							name : 'active',
							width : 100,
							align : "center",
							formatter : booleanFormater
						}],
						loadComplete : function() {
							loadover();
						},
						gridComplete : function() {
						},
						afterInsertRow : function() {
						},
						beforeEditCell : function(rowid, cellname, v, iRow,
								iCol) {
							lastrow = iRow;
							lastcell = iCol;
						}
					});
			initTableRow();
			initEvent();
		})
		
		//设置每行的高度
		function loadover() {
			var grid = $("#grid1");
			var ids = grid.getDataIDs();
			for (var i = 0; i < ids.length; i++) {
				grid.setRowData(ids[i], false, {
					height : 35
				});
			}
		}
		function initTableRow() {
			for (var i = 1; i <= 4; i++) {
				jQuery("#grid1").jqGrid('addRowData', i, {});
			}
			loadover();
		}
		//添加事件
		function initEvent() {
			/* 多条件模糊查询 */
			$(".ui-btn-search").click(function(a) {
				handle.callback({
					"searchField":"active",
					"searchString" : $("#isActive option:selected").attr("value"),
					"dname": $('#dname option:selected').attr('value'),
					"cname": $("#cname").val(),
					"pname": $("#pname").val() 
				});
			}),
			$("#grid1").on("click", ".operating .ui-icon-folder-open", function(a) {
				var rowid = $(this).parent().data("id");
				var rowdata=$("#grid1").jqGrid("getRowData",rowid);
				handle.operate("see",rowid,rowdata);
			}),
			$("#btn-add").click(function(a) {
				handle.operate("add",companyId);
			}), 
			$("#grid1").on("click", ".operating .ui-icon-pencil", function(a) {
				var rowid = $(this).parent().data("id");
				var rowdata=$("#grid1").jqGrid("getRowData",rowid);
				console.log(rowdata);
				handle.operate("edit",rowid,rowdata);
			}),  
			$("#grid1").on("click", ".operating .ui-icon-trash", function(a) {
				a.preventDefault();
				var rowid = $(this).parent().data("id");
				handle.del(rowid);
			}),
			$("#grid").on("click", ".set-status", function(a) {
				var rowid = $(this).data("id");
				alert(rowid);
				var deleted;
				if($(this).hasClass("open")){
					$(this).attr("class","set-status close");
					deleted = 0;
				}else{
					$(this).attr("class","set-status open");
					deleted = 1;
				}
				var post = {
						deleted: deleted,
						id: rowid,
				},
				msg = "状态修改";
				Public.ajaxPost("employee/editActive", post, function(retdata) {
					200 == retdata.status ? (parent.parent.Public.tips({
						content: msg + "成功！"
					}), handle.callback(retdata.data, oper)) : parent.parent.Public.tips({
						type: 1,
						content: msg + "失败！" + retdata.msg
					})
				})
			}),
			$("#btn-record").click(function(a) {
				$("#grid1").jqGrid().trigger("reloadGrid");
			}),$("#btn-setsession").click(function(a){
				$.ajax({
					url:encodeURI("user/setsession"),
					type:"POST",
					success:function(data){
						alert(data);
					}
				});	
			}),$("#btn-getsession").click(function(a){
				$.ajax({
					url:encodeURI("user/getsession"),
					type:"POST",
					success:function(data){
						alert(data);
					}
				});	
			})
		}
		
		handle = {
				operate: function(oper, rowid, rowdata, parentname) {
					var title;
					var data={};
					if ("add" == oper) {
						title = "新增员工信息";
						data={
							operate: oper,
							callback: this.callback,
							defaultCompanyId:defaultCompanyId
						};
						this.dialog = $.dialog({
							title: title,
							data:data,
							content:"url:employee/editjsp",
							width: 480,
							height: 460,
							cache: false,
							lock: true
						})
					} else if("see"==oper){
						this.dialog = $.dialog({
							title: "员工部门调动",
							content:"url:person/seejsp",
							data:{
								operate:oper,
								rowdata:rowdata,
								rowid:rowid,
								callback:this.callback
							},
							width: 300,
							height: 300,
							cache: false,
							lock: true
						});
					}else {
						title = "修改员工信息";
						data={
							operate:oper,
							rowdata:rowdata,
							rowid:rowid,
							callback:this.callback
						};
						this.dialog = $.dialog({
							title: title,
							data:data,
							content:"url:employee/editjsp",
							width: 480,
							height: 460,
							cache: false,
							lock: true
						})
					}
				},
				del: function(rowId) {
					$.dialog.confirm("数据将被永久删除，所有关联数据也会被删除，请确认是否删除？", function() {
						$.ajax({
							url:encodeURI("employee/deleteEmployee"),
							type:"POST",
							data:{
								"id":rowId
							},
							success:function(data){
								window.Public.tips({
									content : "删除成功!"
								})
								$("#grid1").jqGrid().trigger("reloadGrid");	
							}
						});
					});
				},
				callback: function(data) {
					if (data != undefined) {
						$('#grid1').jqGrid('setGridParam', {
							search : true
						});
						$("#grid1").jqGrid("setGridParam", {
							postData : data
						}).jqGrid("setGridParam", {
							page : 1
						}).trigger("reloadGrid");
					} else {
						$("#grid1").jqGrid().trigger("reloadGrid");
					}
				}
			};
	</script>
</body>
</html>